package com.bxait.toymail.product.service.impl;

import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.toymail.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bxait.toymail.product.dao.BrandDao;
import com.bxait.toymail.product.entity.BrandEntity;
import com.bxait.toymail.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Resource
    @Qualifier("categoryBrandRelationService")
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 1.创建构造的QueryWrapper对象
        QueryWrapper<BrandEntity>
                            wrapper = new QueryWrapper<>();
        // 2.获取模糊查询的信息
        String key = (String)params.get("key");

        // 3.构造模糊查询的条件
        if(!StringUtils.isEmpty(key)){
            wrapper.and(item -> item.eq("brand_id", key).or().like("name", key));
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * @param brand
     * @==============History===============<br/>
     * @Description // TODO  修改品牌信息
     * @Date 2023/3/14 21:23 
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return void
     */
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        // 1.更新品牌信息
        this.updateById(brand);
        // 2.如果这个需要更新的品牌信息里携带了品牌名称,我们需要同步的将品牌分类关联表中的品牌名称进行更新
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBatch(brand.getBrandId(),brand.getName());
        }
    }

    /*
     * @param brandIds
     * @==============History===============<br/>
     * @Description //   获取当前品牌id对应的品牌信息
     * @Date 2023/4/16 2023/4/16
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.util.List<java.lang.Long>]
     */
    @Override
    public List<BrandEntity> getBrandByIds(List<Long> brandIds) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("brand_id",brandIds);
        return this.list(queryWrapper);
    }

}